package com.monitor.app_monitor.service.quartz.task;

import com.monitor.app_monitor.config.CircuitMap;
import com.monitor.app_monitor.entity.PmEmsInfo;
import com.monitor.app_monitor.entity.PmScriptExecResult;
import com.monitor.app_monitor.entity.TaskLogAnnotation;
import com.monitor.app_monitor.service.quartz.utils.Dataformat;
import com.monitor.app_monitor.service.quartz.utils.ExecuteScript;
import com.monitor.app_monitor.service.quartz.utils.logUtils;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @ClassName CellectDataCheck.java
 * @Author ning
 * @Description TODO
 * @CreateTime 2021年12月16日 10:18:00
 */

@DisallowConcurrentExecution //作业不并发
@Component
@Slf4j
public class CellectPmFileSizeCheck implements Job {

    @Autowired
    MeterRegistry registry;

    @Resource
    @Qualifier("pmFileSizeMap")
    private  Map<String, AtomicLong> pmFileSizeMap;


    @Resource
    @Qualifier("pmFileNoMap")
    private Map<String, AtomicLong> pmFileNoMap;


    @Resource
    private ExecuteScript executeScript;


    @TaskLogAnnotation
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        /**
         * PM性能文件大小行数统计
         */


        //清除之前的数据
        registry.getMeters().forEach(meter -> {
            if  (meter.getId().getName().equals("pm_file_size") || meter.getId().getName().equals("pm_file_no")) {
                registry.remove(meter.getId());
            }
        });

        try {
            PmEmsInfo pmEmsInfos = executeScript.executePmScript("/home/deployer/bin/PmFileInfo.py");

            System.out.println("pmEmsInfos = " + pmEmsInfos);


            for (PmScriptExecResult pmEmsInfo : pmEmsInfos.getData()) {
                System.out.println("pmEmsInfo = " + pmEmsInfo);

                StringBuilder stringBuilder = new StringBuilder();
                String dataFormat = Dataformat.getDataFormat("yyyyMMdd");
                stringBuilder.append("/data10/YG/PM/");
                stringBuilder.append(dataFormat);
                stringBuilder.append("/");
                stringBuilder.append(pmEmsInfo.getFileName());
                stringBuilder.append("/");
                stringBuilder.append(pmEmsInfo.getEmsName());

                Long resNumberCode = 0L;
                Long resSizeCode = 0L;
                String filename = "";
                if (pmEmsInfo.getEmsName().equals("") || pmEmsInfo.getEmsName() == null){
                    filename = "";
                } else {
                    filename = pmEmsInfo.getEmsName();
                }

                try {
                    File rgFile = new File(stringBuilder.toString());
                    if (rgFile.isFile()) {
                        resNumberCode = ExecuteScript.getLineNumber(rgFile);
                        resSizeCode = rgFile.length();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                log.info("pm大小 {} ,pm行数 {} ,pm名字 {}",resSizeCode,resNumberCode,filename);

                Map<String, String> emsMap = CircuitMap.emsMap;
                List<Tag> tags = new ArrayList<>();
                tags.add(new ImmutableTag("emsName",pmEmsInfo.getFileName()));
                tags.add(new ImmutableTag("fileName",pmEmsInfo.getEmsName()));
                tags.add(new ImmutableTag("emsIp",emsMap.get(pmEmsInfo.getFileName())));
                AtomicLong fileNo = registry.gauge("pm_file_no", tags, new AtomicLong(resNumberCode));
                AtomicLong fileSizlist = registry.gauge("pm_filesize", tags, new AtomicLong(resSizeCode));



                pmFileSizeMap.put(filename,fileSizlist);
                pmFileNoMap.put(filename,fileNo);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }




        log.info("打印pmFileSizeMap");
        pmFileSizeMap.forEach((k,v)->{
            log.info("pmFileSizeMapk = " + k + " v = " + v);
        });
        log.info("打印pmFileNoMap");
        pmFileNoMap.forEach((k,v)->{
            log.info("pmFileNoMapk = " + k + " v = " + v);
        });

    }

}
